package com.sunday.scoremanager.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Message;
import com.sunday.scoremanager.http.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/11/9.
 */

public class MyMessageActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.img1)
    ImageView img1;
    @Bind(R.id.title1)
    TextView title1;
    @Bind(R.id.content1)
    TextView content1;
    @Bind(R.id.img2)
    ImageView img2;
    @Bind(R.id.title2)
    TextView title2;
    @Bind(R.id.content2)
    TextView content2;

    private List<Message> dataSet = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        ButterKnife.bind(this);
        titleView.setText("消息");
        getRecent();
    }

    private void getRecent() {
        Call<ResultDO<List<Message>>> call = ApiClient.getApiAdapter().getRecentMsg(BaseApp.getInstance().getMember().getSellerId());
        call.enqueue(new Callback<ResultDO<List<Message>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Message>>> call, Response<ResultDO<List<Message>>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    dataSet.clear();
                    dataSet.addAll(response.body().getResult());
                    updateView();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Message>>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private void updateView() {
        if (dataSet.isEmpty()) {
            return;
        }

        content1.setText(dataSet.get(0).getMessage());
        content2.setText(dataSet.size() > 1 ? dataSet.get(1).getMessage() : "");

    }

    @OnClick({R.id.rl_msg1, R.id.rl_msg2})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_msg1:
                //收银助手
                intent = new Intent(mContext, MessageListActivity.class);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            case R.id.rl_msg2:
                //系统消息
                intent = new Intent(mContext, MessageListActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
        }
    }
}
